#ifndef _EARBUD_CMD_H_
#define _EARBUD_CMD_H_

#include "main.h"

typedef enum
{
    EAR_L = 0,
    EAR_R,
	EAR_NULL = 0xff,
}EARBUD_TYPE_E;

#if(SUPPORT_UART_TA006_CMD)
//TA006
typedef enum{
    CMD_NULL = 0x00,
    CMD_OPENCASE = 0x01,
    CMD_CLOSECASE = 0x02,
    CMD_POWEROFF = 0x03,
    CMD_BATTERY = 0x04,
    CMD_PAIRING = 0x05,
    CMD_CLEAN_RECORD = 0x06,
    CMD_FACTORY_RESET_KEEP_WWS = 0x07,
    CMD_GET_MAC = 0x08,
    CMD_SET_MAC = 0x09,
    CMD_CLOSECASE_PAIRING = 0x0A,

    //factory test command
    CMD_MONO_MODE = 0x0b,
    CMD_DUT_MODE = 0x0c,
    CMD_SHUTDOWN1 = 0x0d,
    CMD_SHUTDOWN = 0xF5,
    CMD_ENTER_SHIPPING_MODE = 0x0f,
    CMD_UPGRADE = 0x99,
}EARBUD_CMD_E;

#endif

#if(SUPPORT_UART_TA012_CMD)
//TA012
typedef enum{
    CMD_NULL = 0x00,
    CMD_OPENCASE = 0x01,
    CMD_CLOSECASE = 0x02,
    CMD_POWEROFF = 0x03,
    CMD_FACTORY_RESET_KEEP_WWS = 0x05,
    CMD_BATTERY = 0x08,
    CMD_PAIRING = 0x09,
    CMD_GET_MAC = 0x20,
    CMD_SET_MAC = 0x21,
    CMD_CLOSECASE_PAIRING = 0x0a,

    //factory test command
    CMD_MONO_MODE = 0x0b,
    CMD_DUT_MODE = 0x0c,
    CMD_SHUTDOWN = 0xF5,
    CMD_ENTER_SHIPPING_MODE = 0x0f,
    CMD_UPGRADE = 0x99,
}EARBUD_CMD_E;
#endif

typedef enum{
    ACTION_BATTERY = 0x00,
    ACTION_PAIR,
    ACTION_RESET,
}EARBUD_ACTION_E;


typedef enum{
    EAR_OUT_BOX = 0,
	EAR_IN_BOX,
}EARBUD_STAUTS_E;

typedef struct
{
	uint8_t box_status[2];
	uint8_t bt_status[2];
	uint8_t bat[2];
	uint8_t load_addr[2][6];
    uint8_t peer_addr[2][6];
    uint8_t printf_addr_flag[2];
}EAR_STATE_INFO;


#define EAR_CMD_COUNT       8

typedef struct
{
    uint8_t cmd[EAR_CMD_COUNT];
    uint8_t timeout_count[EAR_CMD_COUNT];

    uint8_t head;
    uint8_t tail;
    uint8_t size;
    
    void(*callback)(void);
}EAR_CMD_INFO;


#define EAR_L_ROLE_CMD_FLAG         0x11
#define EAR_R_ROLE_CMD_FLAG         0x22

#define EAR_CHARGE_RESET_TIME       20000   //ms

#define EAR_COMM_INTERVAL_TIME      300     //ms
#define EAR_COMM_TIMEOUT_TIME       100     //ms
#define EAR_COMM_TIMEOUT_COUNT      12
#define EAR_COMM_ACTIVATE_TIME      100     //ms    5V activate time



void earbud_cmd_init(void);
void earbud_cmd_clear(uint8_t role);
void earbud_cmd_next(uint8_t role);


void printf_ear_cmd_array(uint8_t* buf, uint8_t len);
void printf_array(uint8_t* buf, uint8_t len);
void earbud_comm_send_data(uint8_t *data, uint8_t len);
uint8_t earbud_data_decode(uint8_t ear_role, uint8_t* buf, uint8_t len);
uint8_t earbud_cmd_decode(uint8_t ear_role, uint8_t* buf, uint8_t len);
void earbud_cmd_handle(uint8_t msg_id, uint8_t* buf, uint8_t len);

void earbud_comm_send_cmd(uint8_t role, uint8_t cmd);

void earbud_comm_delay_timer_stop(uint8_t role);


uint8_t earbud_check_is_in_box(uint8_t role);
void earbud_comm_timeout_handle(uint8_t role);


void earbud_l_comm_timeout_timer_stop(void);
void earbud_r_comm_timeout_timer_stop(void);

void earbud_l_comm_timer_stop(void);
void earbud_r_comm_timer_stop(void);

uint8_t earbud_comm_is_activity(uint8_t role);
void earbud_delay_start_comm(uint8_t role);


void earbud_enter_pair(void);
void earbud_factory_reset(void);
#if (SUPPORT_UART_AUTO_TWS_PAIR)
void earbud_auto_tws_pair_check(void);
#endif

void earbud_start_comm(uint8_t role);
void earbud_stop_comm(uint8_t role);

void earbud_state_clear(uint8_t role);

void earbud_comm_init(void);
void earbud_comm_deinit(void);

#endif

